package com.sugar.psych.assessment.module.business.controller.admin.studentauth;

import com.sugar.psych.assessment.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.studentauth.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.studentauth.StudentAuthDO;
import com.sugar.psych.assessment.module.business.service.studentauth.StudentAuthService;

@Tag(name = "管理后台 - 学生认证信息")
@RestController
@RequestMapping("/business/student-auth")
@Validated
public class StudentAuthController {

    @Resource
    private StudentAuthService studentAuthService;

    @PostMapping("/create")
    @Operation(summary = "创建学生认证信息")
    @PreAuthorize("@ss.hasPermission('business:student-auth:create')")
    public CommonResult<Long> createStudentAuth(@Valid @RequestBody StudentAuthSaveReqVO createReqVO) {
        return success(studentAuthService.createStudentAuth(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学生认证信息")
    @PreAuthorize("@ss.hasPermission('business:student-auth:update')")
    public CommonResult<Boolean> updateStudentAuth(@Valid @RequestBody StudentAuthSaveReqVO updateReqVO) {
        studentAuthService.updateStudentAuth(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学生认证信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:student-auth:delete')")
    public CommonResult<Boolean> deleteStudentAuth(@RequestParam("id") Long id) {
        studentAuthService.deleteStudentAuth(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除学生认证信息")
                @PreAuthorize("@ss.hasPermission('business:student-auth:delete')")
    public CommonResult<Boolean> deleteStudentAuthList(@RequestParam("ids") List<Long> ids) {
        studentAuthService.deleteStudentAuthListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学生认证信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:student-auth:query')")
    public CommonResult<StudentAuthRespVO> getStudentAuth(@RequestParam("id") Long id) {
        StudentAuthDO studentAuth = studentAuthService.getStudentAuth(id);
        return success(BeanUtils.toBean(studentAuth, StudentAuthRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学生认证信息分页")
    @PreAuthorize("@ss.hasPermission('business:student-auth:query')")
    public CommonResult<PageResult<StudentAuthRespVO>> getStudentAuthPage(@Valid StudentAuthPageReqVO pageReqVO) {
        return success(studentAuthService.getStudentAuthPage(pageReqVO));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入用户认证信息")
    @PreAuthorize("@ss.hasPermission('business:user:import')")
    public CommonResult<Boolean> createUser(FileUploadReqVO uploadReqVO) throws IOException {
        studentAuthService.importExcel(uploadReqVO.getFile());
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学生认证信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:student-auth:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStudentAuthExcel(@Valid StudentAuthPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "学生认证信息.xls", "数据", StudentAuthRespVO.class,
                studentAuthService.getStudentAuthPage(pageReqVO).getList());
    }

}